package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.OrderDetail;
import event.MainPageEvent;
import event.OrderEvent;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;

/**
 * author : ChenHuiMing
 * date : 2018/9/25
 * email : 907399255@qq.com
 * description : 订单详情
 */
public class OrderDetailFragment extends BaseNetFragment {

    public static final String GET_ORDER_URL = "/Shop/getOrder";

    private TextView mTvBack;
    private ImageView mIvLogo;
    private TextView mTvName;
    private TextView mTvTeacher;
    private TextView mTvOrderTime;
    private TextView mTvOrderNo;
    private TextView mTvCoursePrice;
    private TextView mTvPayMoney;
    private TextView mTvGo;
    private LinearLayout mLlCourseMoney;
    private TextView mTvCourseMoney;
    private TextView mTvPayStatus;

    private OrderDetail mOrderDetail;

    public static Fragment getInstance() {
        return new OrderDetailFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, GET_ORDER_URL)) {
            mOrderDetail = (OrderDetail) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), OrderDetail.class);
            if (baseBean.code == 200) {
                ImageLoaderUtils.loadImage(getActivity(), mOrderDetail.schedule_image, mIvLogo);
                mTvName.setText(mOrderDetail.schedule_name);
                mTvTeacher.setText("主讲：" + mOrderDetail.teacher_name);
                mTvOrderTime.setText("订单生成时间:" + mOrderDetail.create_time);
                mTvOrderNo.setText("订单号:" + mOrderDetail.order_no);
                mTvCoursePrice.setText("￥" + mOrderDetail.price);
                mTvPayMoney.setText("￥" + mOrderDetail.amount);
                mTvPayStatus.setText("已支付");
            } else {
                Toast.makeText(getActivity(), baseBean.getMsg(), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_detail;
    }

    @Override
    protected void findViews() {
        mTvBack = getView(R.id.tv_back);
        mIvLogo = getView(R.id.iv_logo);
        mTvName = getView(R.id.tv_name);
        mTvTeacher = getView(R.id.tv_teacher);
        mTvOrderTime = getView(R.id.tv_order_time);
        mTvOrderNo = getView(R.id.tv_order_no);
        mTvCoursePrice = getView(R.id.tv_course_price);
        mTvPayMoney = getView(R.id.tv_pay_money);
        mTvGo = getView(R.id.tv_go);
        mLlCourseMoney = getView(R.id.ll_course_money);
        mTvCourseMoney = getView(R.id.tv_course_money);
        mTvPayStatus = getView(R.id.tv_pay_status);
    }

    @Override
    protected void formatViews() {
        EventBus.getDefault().register(this);
        mTvBack.setOnClickListener(this);
        mTvGo.setOnClickListener(this);
        mLlCourseMoney.setVisibility(View.GONE);
        mTvPayStatus.setVisibility(View.VISIBLE);
    }

    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        if (view == mTvGo) {
            //首页课程
            MainPageEvent pageEvent = new MainPageEvent();
            pageEvent.firstPage = MainPageEvent.HOME_PAGE;
            pageEvent.secondPage = 5;
            EventBus.getDefault().post(pageEvent);
        } else if (view == mTvBack) {
            OrderEvent orderEvent = new OrderEvent();
            orderEvent.pagerIndex = 0;
            EventBus.getDefault().post(orderEvent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderEvent event) {
        if (event.pagerIndex == 2) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", event.order_id);
            post(GET_ORDER_URL, map, BaseBean.class, false);
        }
    }
}
